var gulp = require("gulp"),
	webserver = require("gulp-webserver"),
	livereload =require("gulp-livereload"),
	rename = require("gulp-rename"),
	// sass =require("gulp-ruby-sass"),
	sass =require("gulp-sass"),
	uglify =require("gulp-uglify"),
	html =require("gulp-htmlmin"),
	images =require("gulp-imagemin");

//自动更新
gulp.task("webserver",function(){
	gulp.src("dist")
		.pipe(webserver({
			livereload:true,
			open:true,
			port:8080
		}))
})

//把开发环境发布到发布环境
gulp.task("html",function(){
	return gulp.src("src/**/*.html")
		.pipe(html())
		.pipe(gulp.dest("dist/"))
})

//js文件编辑
gulp.task("js",function(){
	return gulp.src("src/js/*.js")
		.pipe(uglify())
		.pipe(rename({suffix:".min"}))
		.pipe(gulp.dest("dist/js"))
})

//sass 文件编译
gulp.task("sass",function(){
	return gulp.src("src/sass/*.scss"/*,{style:"compressed"}*/)
		.on("error",function(err){
			console.log(err)
		})
		.pipe(sass())
		.pipe(gulp.dest("dist/css"))
})

//图片压缩
gulp.task("images",function(){
	return gulp.src("src/images/*.{png,jpg,gif,svg}")
		.pipe(images({
			progressive:true,
			svgoplugins:[{removeViewBox:false}]
		}))
		.pipe(gulp.dest("dist/images"))
})

//监听任务
gulp.task("watch",function(){
	gulp.watch("src/**/*.html",["html"]);
	gulp.watch("src/sass/*.scss",["sass"]);
	gulp.watch("src/js/*.js",["js"]);
})

//自动默认执行
gulp.task("default",["webserver","js","html","sass","images","watch"])